﻿/*
Copyright (c) 2011 <a href="http://www.gutgames.com">James Craig</a>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.*/

#region Usings
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using DotExtension.Interfaces;
using Utilities.DataTypes;
using Utilities.DataTypes.Patterns.BaseClasses;
#endregion

namespace DotExtension
{
    /// <summary>
    /// Manager for DotExtension
    /// </summary>
    public class Manager : Singleton<Manager>
    {
        #region Constructor

        /// <summary>
        /// Constructor
        /// </summary>
        protected Manager()
            : base()
        {
            Extensions = new System.Collections.Generic.List<IExtension>();
        }

        #endregion

        #region Public Functions

        /// <summary>
        /// Sets up extensions found in a directory
        /// </summary>
        /// <param name="Directory">Directory to search</param>
        public void Setup(string Directory)
        {
            LoadExtensions(Directory);
        }

        /// <summary>
        /// Sets up extensions found in an assembly
        /// </summary>
        /// <param name="Assembly">Assembly to search within</param>
        public void Setup(Assembly Assembly)
        {
            LoadExtensions(Assembly);
        }

        /// <summary>
        /// Sets up extensions found in a list of assemblies
        /// </summary>
        /// <param name="Assemblies">Assemblies to search within</param>
        public void Setup(System.Collections.Generic.List<Assembly> Assemblies)
        {
            foreach (Assembly Assembly in Assemblies)
            {
                LoadExtensions(Assembly);
            }
        }

        /// <summary>
        /// Gets all extensions that are loaded of a specified type
        /// </summary>
        /// <typeparam name="T">Type to search for</typeparam>
        /// <returns>An list of the extensions of the specified type</returns>
        public IEnumerable<T> GetExtensions<T>()
        {
            return Extensions.OfType<T>();
        }

        #endregion

        #region Private Functions

        private void LoadExtensions(string Directory)
        {
            ListMapping<Assembly, Type> Types = Utilities.Reflection.Reflection.GetTypesFromDirectory(Directory, "IExtension");
            foreach (Assembly Assembly in Types.Keys)
            {
                foreach (Type Type in Types[Assembly])
                {
                    Extensions.Add((IExtension)Assembly.CreateInstance(Type.FullName));
                }
            }
        }

        private void LoadExtensions(Assembly Assembly)
        {
            System.Collections.Generic.List<Type> Types = Utilities.Reflection.Reflection.GetTypes(Assembly, "IExtension");
            foreach (Type Type in Types)
            {
                Extensions.Add((IExtension)Type.Assembly.CreateInstance(Type.FullName));
            }
        }

        #endregion

        #region Properties

        /// <summary>
        /// List of extensions that are currently loaded
        /// </summary>
        public System.Collections.Generic.List<IExtension> Extensions { get; set; }

        #endregion
    }
}